<?php
/**
 * Created by 项目.
 * User: xuguopeng
 * Date: 2020-06-22
 * Time: 18:50
 * 退款操作
 */

namespace app\api\service;


use app\api\model\HotelRoomOrder;
use app\lib\exception\ParameterException;

class WxRefund extends Token
{
    /**
     * 1.判断用户信息
     * 2.判断订单信息
     * 3.判断
     * 4.判断订单时间 得出退款金额进行退款操作
     */
    // 用户ID
    private $uid;

    // 订单信息
    private $order;

    // 退款金额
    private $price;

    function __construct($orderId)
    {
        $this->uid = self::getCurrentUid();
        self::judgeMember();
        $this->order = HotelRoomOrder::get($orderId);
    }

    // 入口方法
    public function entrance()
    {
        return $this->judgeOrder();
    }

    // 判断订单
    private function judgeOrder()
    {
        if($this->order['status'] != 2) {
            throw new ParameterException([
                'msg' => [
                    'room_order' => '该订单该状态不可退款'
                ]
            ]);
        }
        if($this->order['user_id'] != $this->uid) {
            throw new ParameterException([
                'msg' => [
                    'room_order' => '不是此用户的订单'
                ]
            ]);
        }
        $this->changeStatus();
        return writeJson(201, [], '申请成功！');
    }

    // 判断时间并得出退款金额
//    private function judgeData()
//    {
//        $now_time = time();
//        $order_time = strtotime($this->order['start_date'].' 12:00:00') - 24*3600;
//        // 还未到扣手续费的时候
//        if($now_time <= $order_time) {
//            $price = $this->order['price'];
//        } else {
//            $price = round($this->order['price'] * 0.7,2);
//        }
//        HotelRoomOrder::where('id',$this->order['id'])->update([
//            'refund_price' => $price
//        ]);
//        $this->price = round($price,2);
//        if($this->price == 0) {
//           $this->changeStatus();
//            return writeJson(201, [], '退款成功！');
//        } else {
//           return $this->orderRefund();
//        }
//    }
//
//    // 进行退款操作
//    private function orderRefund()
//    {
//        $wxOrderData = new \WxPayRefund();
//        $wxOrderData->SetOut_trade_no($this->order['out_trade_no']);
//        $wxOrderData->SetOut_refund_no($this->order['out_refund_no']);
//        $wxOrderData->SetTotal_fee((int)(round($this->order['price']+$this->order['deposit'],2)*100));
//        $wxOrderData->SetRefund_fee((int)($this->price*100));
//        $wxOrderData->SetOp_user_id(config('wx.op_user_id'));
//        $config = new \WxPayConfig();
//        $order_info = \WxPayApi::refund($config, $wxOrderData);
//        if ($order_info['return_code'] == 'SUCCESS' && $order_info['result_code'] == 'SUCCESS') {
//            $this->changeStatus();
//            return writeJson(201, [], '退款成功！');
//        } else {
//            throw new ParameterException([
//                'msg' => [
//                    'room_order' => $order_info['return_msg']
//                ]
//            ]);
//        }
//    }

    // 修改状态
    private function changeStatus()
    {
        HotelRoomOrder::where('id',$this->order['id'])->update([
            'refund_time' => time(),
            'status' => 3,
        ]);
    }

}